﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


static class DynamicExtensions
{
    //static void Main(string[] args)
    //{
    //    Func<int, int> f3 = FuncPow(3, Identity);
    //    Console.WriteLine(f3(4));
    //    Console.WriteLine(Compose(Square, Square)(2));
    //    StringBuilder sb = new StringBuilder("0123456789");
    //    Console.WriteLine(sb.SubStringBuilder(5, 2));
    //}

    static int Identity(int i)
    {
        return i;
    }

    static int Square(int i)
    {
        return i * i;
    }
    static Func<int, int> Multiply(int x, Func<int, int> f)
    {
        Func<int, int> res = t => t * f(x);
        return res;
    }

    static Func<int, int> Multiply(Func<int, int> f1, Func<int, int> f2)
    {
        return x => f1(x) * f2(x);
    }

    static Func<int, int> FuncPow(int n, Func<int, int> f)
    {
        if (n == 0)
        {
            return x => 1;
        }
        Func<int, int> res = f;
        while (n > 1)
        {
            res = Multiply(res, f);
            n--;
        }
        return res;
    }

    static StringBuilder SubStringBuilder(this StringBuilder stringBuilder, int index, int length)
    {
        return new StringBuilder(stringBuilder.ToString().Substring(index, length));
    }

    static Func<int, int> Compose(Func<int, int> f1, Func<int, int> f2)
    {
        return x => f1(f2(x));
    }
}

